/*
 * UseActionBeanCustomizer.java
 *
 * Created on January 23, 2009, 7:31 PM
 */

package org.netbeans.modules.web.stripes.palette.items;

import org.netbeans.modules.web.stripes.palette.CodeSnippet;
import org.netbeans.modules.web.stripes.palette.CodeSnippetCustomizer;
import javax.swing.text.JTextComponent;
import org.netbeans.modules.web.stripes.palette.StripesPaletteUtilities;
import org.netbeans.modules.web.stripes.util.StringUtils;
import org.netbeans.modules.web.stripes.util.browse.BrowseDialogs;



/**
 *
 * @author  Josef Sustacek
 */
public class UseActionBeanCustomizer extends CodeSnippetCustomizer {

    private JTextComponent target;
    
    /** Creates new form UseActionBeanCustomizer */
    public UseActionBeanCustomizer(CodeSnippet codeSnippet, JTextComponent target) {
        super(codeSnippet);
        
        this.target = target;
                
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        buttonGroup = new javax.swing.ButtonGroup();
        bindingButton = new javax.swing.JRadioButton();
        beanclassButton = new javax.swing.JRadioButton();
        bindingText = new javax.swing.JTextField();
        beanclassText = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        varText = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        buttonGroup.add(bindingButton);
        bindingButton.setSelected(true);
        bindingButton.setText(org.openide.util.NbBundle.getMessage(UseActionBeanCustomizer.class, "UseActionBeanCustomizer.bindingButton.text")); // NOI18N

        buttonGroup.add(beanclassButton);
        beanclassButton.setText(org.openide.util.NbBundle.getMessage(UseActionBeanCustomizer.class, "UseActionBeanCustomizer.beanclassButton.text")); // NOI18N

        bindingText.setText(org.openide.util.NbBundle.getMessage(UseActionBeanCustomizer.class, "UseActionBeanCustomizer.bindingText.text")); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, bindingButton, org.jdesktop.beansbinding.ELProperty.create("${selected}"), bindingText, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        bindingText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bindingTextActionPerformed(evt);
            }
        });

        beanclassText.setText(org.openide.util.NbBundle.getMessage(UseActionBeanCustomizer.class, "UseActionBeanCustomizer.beanclassText.text")); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, beanclassButton, org.jdesktop.beansbinding.ELProperty.create("${selected}"), beanclassText, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jLabel1.setText(org.openide.util.NbBundle.getMessage(UseActionBeanCustomizer.class, "UseActionBeanCustomizer.jLabel1.text")); // NOI18N

        varText.setText(org.openide.util.NbBundle.getMessage(UseActionBeanCustomizer.class, "UseActionBeanCustomizer.varText.text")); // NOI18N

        jButton1.setText(org.openide.util.NbBundle.getMessage(UseActionBeanCustomizer.class, "UseActionBeanCustomizer.jButton1.text")); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, beanclassButton, org.jdesktop.beansbinding.ELProperty.create("${selected}"), jButton1, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bindingButton)
                    .addComponent(beanclassButton)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(varText)
                    .addComponent(beanclassText)
                    .addComponent(bindingText, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(varText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bindingButton)
                    .addComponent(bindingText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(beanclassButton)
                    .addComponent(beanclassText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

private void bindingTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bindingTextActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_bindingTextActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    String beanFQN = BrowseDialogs.javaSourcesDialog(
            StripesPaletteUtilities.getProject(target), this);
    
    if(!StringUtils.nullOrEmpty(beanFQN)){
        beanclassText.setText(beanFQN);
    }
}//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton beanclassButton;
    private javax.swing.JTextField beanclassText;
    private javax.swing.JRadioButton bindingButton;
    private javax.swing.JTextField bindingText;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField varText;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    @Override
    protected void evaluateInput() {
        codeSnippet.setProperty(UseActionBean.VAR, varText.getText());
        
        if(bindingButton.isSelected()){
            codeSnippet.setProperty(UseActionBean.BINDING, bindingText.getText());
            codeSnippet.setProperty(UseActionBean.BEAN_CLASS, null);
        }
        
        if(beanclassButton.isSelected()){
            codeSnippet.setProperty(UseActionBean.BEAN_CLASS, beanclassText.getText());
            codeSnippet.setProperty(UseActionBean.BINDING, null);
        }
    }
}
